package com.upload.controller;

import com.alibaba.fastjson.JSON;
import com.upload.bean.yCoin.LoginInfoBean;
import com.upload.service.LoginService;
import com.upload.util.CookieUtil;
import com.upload.util.JSONUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by asus on 2017/3/2.
 */
@RestController
@RequestMapping(value = "/api")
public class LoginController {

    @Autowired
    private LoginService loginService;

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public LoginInfoBean loadResponse(@RequestParam("code") String code, HttpServletRequest request, HttpServletResponse response){
        System.out.println(request.getScheme()+"://"+ request.getServerName() + ":" + request.getServerPort()+request.getRequestURI()+"?"+request.getQueryString());
        System.out.println("login");
        System.out.println("body = " + code);

        System.out.println("requestUri = " + request.getRequestURI());
        System.out.println("requestQueryString = " + request.getQueryString());
        System.out.println("requestServletPath = " + request.getServletPath());
        System.out.println("requestContextPath = " + request.getContextPath());
        System.out.println("requestRemoteUser = " + request.getRemoteUser());

//        String code = (String) JSONUtil.getStringFromJSONObject(body, "code");
//        System.out.println("code = " + code);

//        LoginInfoBean loginInfoBean = new LoginInfoBean();
//        loginInfoBean.setStatus("102");
//        loginInfoBean.setRole("admin");
//        loginInfoBean.setUnionid("hahahha");
//        loginInfoBean.setMsg("try try");
//        loginInfoBean.setHeadImgUrl("http://wx.qlogo.cn/mmopen/PiajxSqBRaEJzR907rYxDSM07t2qZkRZoZNdZhSzy2yuLBGhhekAQCnW7yBTgdLbs8EaNSibdbua7N6NYKK7srFQ/0");

//        CookieUtil cookieUtil = new CookieUtil();
//        cookieUtil.setCookie("hy", "66666666666", 100, response);

        LoginInfoBean loginInfoBean = loginService.loginHandler(response, code);

        System.out.println(JSONUtil.toJsonStr(loginInfoBean));

        return loginInfoBean;
    }

    @RequestMapping(value="/test")
    public void testlogin(){
        System.out.println("test");
    }

}
